package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.mwsreportbean.MwsActiveListingsReport;
import com.yunchang.springboot.models.mwsreportbean.MwsActiveListingsReportDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.scheduled.mwsreport.ReportDateDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Integer.parseInt;
import static java.lang.String.valueOf;

/**
 * @author czw
 */
@Component
public class ActiveListingsReportDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    @Resource
    ReportDateDispose reportDateDispose;

    /**
     * 在售商品报告  将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopName
     * @param area
     * @return
     * @throws Exception
     */
    public List<Object> reportDataDispose(List<List<String>> lists, String shopName, String area,String sellerId) throws Exception {
        // 原数据插入数据库的集合
        List<Object> insertList = new ArrayList<>(lists.size());
        // 原数据处理集合
        List<MwsActiveListingsReportDO> mwsActiveListingsReportList = new ArrayList<>(lists.size());
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        // 不用区域的表头有所不同，目前是日本区域的不同需要做额外处理
        if (null != area && area.equals(ESPECIALLY_AREA) && !shopName.equals(SPECIAL_SHOP_NAME)) {
            int itemNameIndex = parseInt(valueOf(titleMap.get("商品名")));
            int listingIdIndex = parseInt(valueOf(titleMap.get("出品ID")));
            int sellerSkuIndex = parseInt(valueOf(titleMap.get("出品者SKU")));
            int priceIndex = parseInt(valueOf(titleMap.get("価格")));
            int quantityIndex = parseInt(valueOf(titleMap.get("数量")));
            int openDateIndex = parseInt(valueOf(titleMap.get("出品日")));
            int productIdTypeIndex = parseInt(valueOf(titleMap.get("商品IDタイプ")));
            int itemNoteIndex = parseInt(valueOf(titleMap.get("コンディション説明")));
            int itemConditionIndex = parseInt(valueOf(titleMap.get("コンディション")));
            int willShipInternationallyIndex = parseInt(valueOf(titleMap.get("国外へ配送可")));
            int expeditedShippingIndex = parseInt(valueOf(titleMap.get("迅速な配送")));
            int productIdIndex = parseInt(valueOf(titleMap.get("商品ID")));
            int pendingQuantityIndex = parseInt(valueOf(titleMap.get("在庫数")));
            int fulfillmentChannelIndex = parseInt(valueOf(titleMap.get("フルフィルメント・チャンネル")));
            int statusIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("ステータス")) {
                statusIndex = parseInt(valueOf(titleMap.get("ステータス")));
            }
            int merchantShippingGroupIndex = parseInt(valueOf(titleMap.get("merchant-shipping-group")));

            for (int i = 1; i < lists.size(); i++) {
                MwsActiveListingsReportDO mwsActiveListingsReport = new MwsActiveListingsReportDO();
                List<String> row = lists.get(i);
                mwsActiveListingsReport.setSellerId(sellerId);
                mwsActiveListingsReport.setArea(area);
                mwsActiveListingsReport.setItemName(row.get(itemNameIndex));
                mwsActiveListingsReport.setListingId(row.get(listingIdIndex));
                mwsActiveListingsReport.setSellerSku(row.get(sellerSkuIndex));
                mwsActiveListingsReport.setPrice(new BigDecimal(valueOf(row.get(priceIndex))));
                mwsActiveListingsReport.setQuantity(ToolUtil.valueOfInteger(row.get(quantityIndex)));
                if (row.get(openDateIndex).contains("/")) {
                    mwsActiveListingsReport.setOpenDate(DateUtil.ISO8601ToGMTByOtherPattern(row.get(openDateIndex)));
                } else {
                    mwsActiveListingsReport.setOpenDate(DateUtil.ISO8601ToGMTByOtherPatternTwo(row.get(openDateIndex)));
                }
                mwsActiveListingsReport.setProductIdType(row.get(productIdTypeIndex));
                mwsActiveListingsReport.setItemNote(row.get(itemNoteIndex));
                mwsActiveListingsReport.setItemCondition(row.get(itemConditionIndex));
                mwsActiveListingsReport.setWillShipInternationally(row.get(willShipInternationallyIndex));
                mwsActiveListingsReport.setExpeditedShipping(row.get(expeditedShippingIndex));
                mwsActiveListingsReport.setProductId(row.get(productIdIndex));
                mwsActiveListingsReport.setPendingQuantity(ToolUtil.valueOfInteger(row.get(pendingQuantityIndex)));
                mwsActiveListingsReport.setFulfillmentChannel(row.get(fulfillmentChannelIndex));
                mwsActiveListingsReport.setMerchantShippingGroup(row.get(merchantShippingGroupIndex));
                mwsActiveListingsReport.setCreateTime(DateUtil.getDate());
                mwsActiveListingsReport.setUpdateTime(DateUtil.getDate());
                if (statusIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setStatus(row.get(statusIndex));
                }
                mwsActiveListingsReport.setFinanceStatus(1);
                insertList.add(mwsActiveListingsReport);
//                mwsActiveListingsReportList.add(mwsActiveListingsReport);
            }
//            reportDateDispose.skuInfoDispose(mwsActiveListingsReportList);
            return insertList;
        } else {
            int itemNameIndex = parseInt(valueOf(titleMap.get("item-name")));
            int itemDescriptionIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("item-description")) {
                itemDescriptionIndex = parseInt(valueOf(titleMap.get("item-description")));
            }
            int listingIdIndex = parseInt(valueOf(titleMap.get("listing-id")));
            int sellerSkuIndex = parseInt(valueOf(titleMap.get("seller-sku")));
            int priceIndex = parseInt(valueOf(titleMap.get("price")));
            int quantityIndex = parseInt(valueOf(titleMap.get("quantity")));
            int openDateIndex = parseInt(valueOf(titleMap.get("open-date")));
            int imageUrlIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("image-url")) {
                imageUrlIndex = parseInt(valueOf(titleMap.get("image-url")));
            }
            int itemIsMarketplaceIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("item-is-marketplace")) {
                itemIsMarketplaceIndex = parseInt(valueOf(titleMap.get("item-is-marketplace")));
            }
            int productIdTypeIndex = parseInt(valueOf(titleMap.get("product-id-type")));
            int zshopShippingFeeIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("zshop-shipping-fee")) {
                zshopShippingFeeIndex = parseInt(valueOf(titleMap.get("zshop-shipping-fee")));
            }
            int itemNoteIndex = parseInt(valueOf(titleMap.get("item-note")));
            int itemConditionIndex = parseInt(valueOf(titleMap.get("item-condition")));
            int zshopCategory1Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("zshop-category1")) {
                zshopCategory1Index = parseInt(valueOf(titleMap.get("zshop-category1")));
            }
            int zshopBrowsePathIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("zshop-browse-path")) {
                zshopBrowsePathIndex = parseInt(valueOf(titleMap.get("zshop-browse-path")));
            }
            int zshopStorefrontFeatureIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("zshop-storefront-feature")) {
                zshopStorefrontFeatureIndex = parseInt(valueOf(titleMap.get("zshop-storefront-feature")));
            }
            int asin1Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("asin1")) {
                asin1Index = parseInt(valueOf(titleMap.get("asin1")));
            }
            int asin2Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("asin2")) {
                asin2Index = parseInt(valueOf(titleMap.get("asin2")));
            }
            int asin3Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("asin3")) {
                asin3Index = parseInt(valueOf(titleMap.get("asin3")));
            }

            int willShipInternationallyIndex = parseInt(valueOf(titleMap.get("will-ship-internationally")));
            int expeditedShippingIndex = parseInt(valueOf(titleMap.get("expedited-shipping")));
            int zshopBoldfaceIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("zshop-boldface")) {
                zshopBoldfaceIndex = parseInt(valueOf(titleMap.get("zshop-boldface")));
            }
            int productIdIndex = parseInt(valueOf(titleMap.get("product-id")));
            int bidForFeaturedPlacementIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("bid-for-featured-placement")) {
                bidForFeaturedPlacementIndex = parseInt(valueOf(titleMap.get("bid-for-featured-placement")));
            }
            int addDeleteIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("add-delete")) {
                addDeleteIndex = parseInt(valueOf(titleMap.get("add-delete")));
            }
            int pendingQuantityIndex = parseInt(valueOf(titleMap.get("pending-quantity")));
            int fulfillmentChannelIndex = parseInt(valueOf(titleMap.get("fulfillment-channel")));
            int BusinessPriceIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Business Price")) {
                BusinessPriceIndex = parseInt(valueOf(titleMap.get("Business Price")));
            }
            int QuantityPriceTypeIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Price Type")) {
                QuantityPriceTypeIndex = parseInt(valueOf(titleMap.get("Quantity Price Type")));
            }
            int QuantityLowerBound1Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Lower Bound 1")) {
                QuantityLowerBound1Index = parseInt(valueOf(titleMap.get("Quantity Lower Bound 1")));
            }
            int QuantityPrice1Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Price 1")) {
                QuantityPrice1Index = parseInt(valueOf(titleMap.get("Quantity Price 1")));
            }
            int QuantityLowerBound2Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Lower Bound 2")) {
                QuantityLowerBound2Index = parseInt(valueOf(titleMap.get("Quantity Lower Bound 2")));
            }
            int QuantityPrice2Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Price 2")) {
                QuantityPrice2Index = parseInt(valueOf(titleMap.get("Quantity Price 2")));
            }
            int QuantityLowerBound3Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Lower Bound 3")) {
                QuantityLowerBound3Index = parseInt(valueOf(titleMap.get("Quantity Lower Bound 3")));
            }
            int QuantityPrice3Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Price 3")) {
                QuantityPrice3Index = parseInt(valueOf(titleMap.get("Quantity Price 3")));
            }
            int QuantityLowerBound4Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Lower Bound 4")) {
                QuantityLowerBound4Index = parseInt(valueOf(titleMap.get("Quantity Lower Bound 4")));
            }
            int QuantityPrice4Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Price 4")) {
                QuantityPrice4Index = parseInt(valueOf(titleMap.get("Quantity Price 4")));
            }
            int QuantityLowerBound5Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Lower Bound 5")) {
                QuantityLowerBound5Index = parseInt(valueOf(titleMap.get("Quantity Lower Bound 5")));
            }
            int QuantityPrice5Index = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("Quantity Price 5")) {
                QuantityPrice5Index = parseInt(valueOf(titleMap.get("Quantity Price 5")));
            }
            int statusIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("status")) {
                statusIndex = parseInt(valueOf(titleMap.get("status")));
            }
            int merchantShippingGroupIndex = WRONG_NUMBER_SIGN;
            if (null != titleMap.get("merchant-shipping-group")) {
                merchantShippingGroupIndex = parseInt(valueOf(titleMap.get("merchant-shipping-group")));
            }
            for (int i = 1; i < lists.size(); i++) {
                MwsActiveListingsReportDO mwsActiveListingsReport = new MwsActiveListingsReportDO();
                List<String> row = lists.get(i);
                mwsActiveListingsReport.setSellerId(sellerId);
                mwsActiveListingsReport.setArea(area);
                mwsActiveListingsReport.setItemName(row.get(itemNameIndex));
                if (itemDescriptionIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setItemDescription(row.get(itemDescriptionIndex));
                }
                mwsActiveListingsReport.setListingId(row.get(listingIdIndex));
                mwsActiveListingsReport.setSellerSku(row.get(sellerSkuIndex));
                mwsActiveListingsReport.setPrice(new BigDecimal(valueOf(row.get(priceIndex))));
                mwsActiveListingsReport.setQuantity(ToolUtil.valueOfInteger(row.get(quantityIndex)));
                if (row.get(openDateIndex).contains("/")) {
                    mwsActiveListingsReport.setOpenDate(DateUtil.ISO8601ToGMTByOtherPattern(row.get(openDateIndex)));
                } else {
                    mwsActiveListingsReport.setOpenDate(DateUtil.ISO8601ToGMTByOtherPatternTwo(row.get(openDateIndex)));
                }
                if (imageUrlIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setImageUrl(row.get(imageUrlIndex));
                }
                if (itemIsMarketplaceIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setItemIsMarketplace(row.get(itemIsMarketplaceIndex));
                }
                mwsActiveListingsReport.setProductIdType(row.get(productIdTypeIndex));
                if (zshopShippingFeeIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setZshopShippingFee(row.get(zshopShippingFeeIndex));
                }
                mwsActiveListingsReport.setItemNote(row.get(itemNoteIndex));
                mwsActiveListingsReport.setItemCondition(row.get(itemConditionIndex));
                if (zshopCategory1Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setZshopCategory1(row.get(zshopCategory1Index));
                }
                if (zshopBrowsePathIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setZshopBrowsePath(row.get(zshopBrowsePathIndex));
                }
                if (zshopStorefrontFeatureIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setZshopStorefrontFeature(row.get(zshopStorefrontFeatureIndex));
                }
                if (asin1Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setAsin1(row.get(asin1Index));
                }
                if (asin2Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setAsin2(row.get(asin2Index));
                }
                if (asin3Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setAsin3(row.get(asin3Index));
                }
                mwsActiveListingsReport.setWillShipInternationally(row.get(willShipInternationallyIndex));
                mwsActiveListingsReport.setExpeditedShipping(row.get(expeditedShippingIndex));
                if (zshopBoldfaceIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setZshopBoldface(row.get(zshopBoldfaceIndex));
                }
                mwsActiveListingsReport.setProductId(row.get(productIdIndex));
                if (bidForFeaturedPlacementIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setBidForFeaturedPlacement(row.get(bidForFeaturedPlacementIndex));
                }
                if (addDeleteIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setAddDelete(row.get(addDeleteIndex));
                }
                mwsActiveListingsReport.setPendingQuantity(ToolUtil.valueOfInteger(row.get(pendingQuantityIndex)));
                mwsActiveListingsReport.setFulfillmentChannel(row.get(fulfillmentChannelIndex));
                if (merchantShippingGroupIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setMerchantShippingGroup(row.get(merchantShippingGroupIndex));
                }
                if (BusinessPriceIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setBusinessPrice(new BigDecimal(valueOf(row.get(BusinessPriceIndex))));
                }
                if (QuantityPriceTypeIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityPriceType(row.get(QuantityPriceTypeIndex));
                }
                if (QuantityLowerBound1Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityLowerBound1(ToolUtil.valueOfInteger(row.get(QuantityLowerBound1Index)));
                }
                if (QuantityPrice1Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityPrice1(new BigDecimal(valueOf(row.get(QuantityPrice1Index))));
                }
                if (QuantityLowerBound2Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityLowerBound2(ToolUtil.valueOfInteger(row.get(QuantityLowerBound2Index)));
                }
                if (QuantityPrice2Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityPrice2(new BigDecimal(valueOf(row.get(QuantityPrice2Index))));
                }
                if (QuantityLowerBound3Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityLowerBound3(ToolUtil.valueOfInteger(row.get(QuantityLowerBound2Index)));
                }
                if (QuantityPrice3Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityPrice3(new BigDecimal(valueOf(row.get(QuantityPrice2Index))));
                }
                if (QuantityLowerBound4Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityLowerBound4(ToolUtil.valueOfInteger(row.get(QuantityLowerBound4Index)));
                }
                if (QuantityPrice4Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityPrice4(new BigDecimal(valueOf(row.get(QuantityPrice4Index))));
                }
                if (QuantityLowerBound5Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityLowerBound5(ToolUtil.valueOfInteger(row.get(QuantityLowerBound5Index)));
                }
                if (QuantityPrice5Index != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setQuantityPrice5(new BigDecimal(valueOf(row.get(QuantityPrice5Index))));
                }
                if (statusIndex != WRONG_NUMBER_SIGN) {
                    mwsActiveListingsReport.setStatus(row.get(statusIndex));
                }
                mwsActiveListingsReport.setCreateTime(DateUtil.getDate());
                mwsActiveListingsReport.setUpdateTime(DateUtil.getDate());
                mwsActiveListingsReport.setFinanceStatus(1);
                insertList.add(mwsActiveListingsReport);
                mwsActiveListingsReportList.add(mwsActiveListingsReport);
            }
//            reportDateDispose.skuInfoDispose(mwsActiveListingsReportList);
            return insertList;
        }
    }
}
